<template>
  <div>
  <vue-particles
      id="particles-js"
      color="#7ec7fd"
      :particleOpacity="0.5"
      :particlesNumber="80"
      shapeType="circle"
      :particleSize="4"

      linesColor="#7ec7fd"
      :linesWidth="1"
      :lineLinked="true"
      :lineOpacity="0.4"
      :linesDistance="150"
      :moveSpeed="2"
      :hoverEffect="false"
      hoverMode="grab"
      :clickEffect="false"
      clickMode="push"
  >
  </vue-particles>
  <div class="wrapper">
    <div style="height: 0px;opacity: 0.8" >
      <h2 style="color: #E6A23C;position:relative;right: -70px;bottom: -8px;width: 220px;height: 42px;line-height: 42px">IKUN后台管理系统</h2>
      <img src="../assets/logo.png" alt="" width="57" style="position: relative;top: -35px;right:-13px;opacity: 0.8" >
    </div>
    <div id="building">
      <div style="margin: 20px 0;text-align: center;font-size: 25px;user-select: none;">
        <h1 style="color: #1E9FFF;font-weight: bold;font-size: 25px">欢迎注册</h1></div>
      <el-form :model="user" :rules="rules" ref="userForm">
        <el-form-item prop="username" >
          <el-input placeholder="请输入用户名" size="medium" style="margin: 5px 0" prefix-icon="el-icon-user" v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item prop="nickname" >
          <el-input placeholder="请输入昵称" size="medium" style="margin: 5px 0" prefix-icon="el-icon-user" v-model="user.nickname"></el-input>
        </el-form-item>
        <el-form-item prop="password" >
          <el-input placeholder="请输入密码"  size="medium" style="margin: 5px 0" prefix-icon="el-icon-lock" show-password v-model="user.password"></el-input>
        </el-form-item>
        <el-form-item prop="confirmpassword" >
          <el-input placeholder="请确认密码" size="medium" style="margin: 5px 0" prefix-icon="el-icon-lock" show-password v-model="user.confirmpassword"></el-input>
        </el-form-item>
      </el-form>

      <div style="margin: 10px 0; text-align: right">
        <el-button type="success" size="large" autocomplete="off" @click="login">注册</el-button>
        <el-button type="warning" size="large" autocomplete="off" @click="$router.push('/login')">返回登录</el-button>
      </div>
    </div>
  </div>
  </div>
</template>
<script>
export default {
  name: "Register",
  data() {
    return {
      user:{},
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {pattern: /^[A-Za-z0-9]+$/, message: "大写、小写字母或数字", trigger: "blur"},
        ],
        nickname: [
          {required: true, message: '请输入昵称', trigger: 'blur'},
          {min: 3, max: 10, message: "长度在3到10个字符", trigger: "blur"},
        ],
        confirmpassword: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {  type: 'string', message: '请输入不包含空格的字符', trigger: 'blur', transform(value) {
              if (value && value.indexOf(' ') === -1) {
                return value
              } else {
                return false
              }
            }
          },],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {pattern: /^[A-Za-z0-9]+$/, message: "大写、小写字母或数字", trigger: "blur"},
          {min: 3, max: 20, message: "长度在1 到 20个字符之间", trigger: 'blur'},
          {  type: 'string', message: '请输入不包含空格的字符', trigger: 'blur', transform(value) {
              if (value && value.indexOf(' ') === -1) {
                return value
              } else {
                return false
              }
            }
          },]
      }
    }
  },
  methods:{
    login(){
      this.$refs['userForm'].validate((valid) =>{
        if(valid){        //表单校验
          if (this.user.password!==this.user.confirmpassword){
            this.$message.error("两次输入的密码不一致")
            return false
          }
          this.request.post("/user/register",this.user).then(res=>{
            if(res.code=='200'){
              this.$message.success("注册成功!3秒后跳转到登录页")
              setTimeout(home =>{
                this.$router.push('/login')
              },2000)
            }else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>
<style scoped >
.wrapper{
  width: 100%;
  height: 100vh;
  background-size: cover;
  overflow: hidden;
  background-image: url("../assets/bg.jpg");
}
#particles-js {
  width: 100%;
  position: absolute;
}
#building{
  position: relative;
  margin: 200px auto;
  background-color:#fff;
  width: 350px;
  height: 400px;
  padding: 20px;
  border-radius: 10px;
  /*为其整体设置接近透明的效果*/
  background-color: rgba(255,255,255,0.4);
  /*设置box更加有立体感*/
  box-shadow: 5px 5px 0 0 rgba(0,0,0,0.2);
}
</style>